// pages/consultants/consultants.js
const util = require("../../utils/util");
const api = require("../../api/api");
Page({

  /**
   * 页面的初始数据
   */
  data: {
      //咨询师列表
      consultants: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '',
    })
    // wx.navigateTo({
    //   url: '/im/pages/index?type=kefu&iskefu=true',
    // })
    wx.navigateTo({
      url: '/im/pages/index?type=consultants&iskefu=false',
    })
    // wx.reLaunch({
    //   url: '/im/pages/index?type=consultants&iskefu=false',
    // })
    return;
    wx.setNavigationBarTitle({
      title: '育儿咨询',
    })
    //老师列表咨询
    this.initConsultants();
  },
  initConsultants() {
    //const api=require("../../api/api");
    const that = this;
    //取得客服账号
    api.im.kefuList({
      success(resKefu) {
        //保存客服列表
        that.setData({
          kefuList: resKefu.data.data,
        })
        // 咨询师
        that.setData({
          consultants: resKefu.data.data.tutor,
        })
        //选择咨询师
        that.setData({
          showSelConsultants: true,
        })
        // //对话对象
        // let to = resKefu.data.data.kefu[0];
        // that.setData({
        //   to: to,
        // })
      }
    })
  },
  /**
   * 点击老师
   * @param {*} e 
   */
  tapTeacher(e) {
    console.log("tapTeacher,e", e)
    let im_account = e.currentTarget.dataset.im_account;
    let index = e.currentTarget.dataset.index;
    const that = this;
    //对话对象
    let to = this.data.consultants[index];
    that.setData({
      to: to,
    })
    //关闭弹出框
    this.setData({
      showSelConsultants: false,
      showChatView: true,
    })
    //获取用户IM账号
    api.im.getUserImAccount({
      to: im_account,
      success(resAcc) {
        // console.log("getUserImAccount,resAcc", resAcc);
        //客户信息
        that.setData({
          from: resAcc.data.data.from_im,
        })
        //初始化
        that.init()
      }
    })
  },
  /**
   * 初始化TIM
   */
  init() {
    const TIM = require('../../im/TUIKit/index.js')
    console.log(TIM);
    const that = this;
    const userSig = that.data.from.user_sig;
    wx.$TUIKit = TIM.create({
      SDKAppID: util.getImSDKAPPID(),
    });
    wx.$chat_SDKAppID = util.getImSDKAPPID();
    wx.$chat_userID = that.data.from.im_account;
    wx.$chat_userSig = userSig;
    wx.$TUIKitTIM = TIM;
    wx.$TUIKit.registerPlugin({
      'tim-upload-plugin': TIMUploadPlugin
    });
    wx.$TUIKit.login({
      userID: that.data.from.im_account,
      userSig
    });
    wx.setStorage({
      key: 'currentUserID',
      data: [],
    });
    wx.$TUIKit.on(wx.$TUIKitTIM.EVENT.SDK_READY, that.onSDKReady, this);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})